//
//  JFCreateBoardCell.m
//  JobFree2.0
//
//  Created by Dale on 2017/5/31.
//  Copyright © 2017年 Dale. All rights reserved.
//

#import "JFCreateBoardCell.h"
#import "JFMemberCell.h"
#import "NSString+JFCategory.h"

#define kItemSize  ((Screen_Width - 55) / 4.0)
@interface JFCreateBoardCell()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIButton *logoBtn;
@property (weak, nonatomic) IBOutlet UILabel *timeLB;
@property (weak, nonatomic) IBOutlet UILabel *nameLB;
@property (weak, nonatomic) IBOutlet UITextField *descTF;
@property (weak, nonatomic) IBOutlet UILabel *membersLB;
@property (weak, nonatomic) IBOutlet UIImageView *line;


@end

@implementation JFCreateBoardCell

static NSString *cellID = @"JFMemberCell";

+ (instancetype)cellWithTableView:(UITableView *)tableView withDic:(NSDictionary *)dic {
    int type = [dic[@"type"] intValue];
    static NSString *identifier = @"JFCreateBoardCell";
    JFCreateBoardCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        switch (type) {
            case 0:
            {
                cell = LoadView(@"JFCreateBoardCell", 0);
            }
            break;
            case 1:
            {
                cell = LoadView(@"JFCreateBoardCell", 1);
            }
            break;
            default:
            {
                cell = LoadView(@"JFCreateBoardCell", 2);
            }
            break;
        }
    }
    return cell;
}

+ (CGFloat)loadCellHeightWithDic:(NSDictionary *)dic {
    if ([dic[@"type"] intValue] == 2) {
        NSArray *array = dic[@"members"];
        return 70.0 + (kItemSize  * ceil((array.count + 2) / 4.0)) + 10.0;
    }
    return 70.0;
}

- (void)setDic:(NSDictionary *)dic {
    _dic = dic;
    self.nameLB.text = dic[@"name"];
    self.descTF.placeholder = dic[@"description"];
    self.membersLB.text = dic[@"description"];
    self.timeLB.hidden = YES;
    if ([dic[@"name"] isEqualToString:@"备注"]) {
        self.line.hidden = YES;
    } else {
        self.line.hidden = NO;
    }
}

- (void)setSetDic:(NSDictionary *)setDic {
    _setDic = setDic;
    if ([setDic[@"type"] intValue] == 0) {
        if ([NSString isEnglishFirst:setDic[@"name"]]) {
            NSString *logoStr = [[setDic[@"name"] substringToIndex:2] capitalizedString];
            [self.logoBtn setTitle:logoStr forState:UIControlStateNormal];
        } else {
            NSString *logoStr = [setDic[@"name"] substringToIndex:1];
            [self.logoBtn setTitle:logoStr forState:UIControlStateNormal];
        }
    }
    
    self.timeLB.hidden = NO;
    self.timeLB.text = setDic[@"description"];
    
    if ([setDic[@"type"] intValue] == 1) {
        self.nameLB.text = setDic[@"name"];
        self.descTF.placeholder = setDic[@"description"];
        self.descTF.text = setDic[@"text"];
    }
    
    self.membersLB.text = setDic[@"description"];
    
    if ([setDic[@"name"] isEqualToString:@"备注"]) {
        self.line.hidden = YES;
    } else {
        self.line.hidden = NO;
    }
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    NSArray *logoArr = [self.board_logo componentsSeparatedByString:@","];
    CGFloat r = [(NSString *)logoArr[0] floatValue];
    CGFloat g = [(NSString *)logoArr[1] floatValue];
    CGFloat b = [(NSString *)logoArr[2] floatValue];
    self.logoBtn.backgroundColor = [UIColor colorWithRed:r green:g blue:b alpha:1.0];
    [self.logoBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    setViewCorner(self.logoBtn, 25);
    self.descTF.delegate = self;
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 0.0;
    flowLayout.minimumInteritemSpacing = 0.0;
    self.collectionView.collectionViewLayout = flowLayout;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
//    self.collectionView.backgroundColor = [UIColor redColor];
    [self.collectionView registerClass:[JFMemberCell class] forCellWithReuseIdentifier:cellID];
    
}

#pragma mark UICollectionViewDelegate && DataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JFUser *user = self.dataSource[indexPath.row];
    JFMemberCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.user = user;
    cell.clickMemberBtnBlock = ^(JFMemberCellType type) {
        if (type == JFMemberCellTypePerson) {
            if (self.clickMemberBlock) {
                self.clickMemberBlock(JFCreateBoardCellTypePerson);
            }
        } else {
            if (self.clickMemberBlock) {
                self.clickMemberBlock(JFCreateBoardCellTypeAdd);
            }
        }
    };
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(kItemSize, kItemSize);
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (self.textFieldBlock) {
        if ([_dic[@"name"] isEqualToString:@"名称"]) {
            self.textFieldBlock(textField.text, 0);
        }else {
            self.textFieldBlock(textField.text, 1);
        }
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
